﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;
using System.ComponentModel;
using Andrewl.XnaHelpers.Guide_Helper;

namespace Andrewl.XnaHelpers.Storage
{
    public class XboxStorage
    {
        #region Variable Declarations

        public StorageContainer container;

        public StorageDevice device;

        public StorageState state;

        #endregion

        #region Accessors

        public StorageContainer Container
        { get { return container; } set { container = value; }  }

        public StorageDevice Device
        { get { return device; } set { device = value; } }

        public string Path
        { get { return container.Path; } }

        public bool IsConnected
        { get { return container.StorageDevice.IsConnected; } }

        public long FreeSpace
        { get { return container.StorageDevice.FreeSpace; } }

        public long UsedSpace
        { get { return (container.StorageDevice.TotalSpace - container.StorageDevice.FreeSpace); } }

        #endregion

        public XboxStorage()
        {
            state = StorageState.Null;
        }

        public void GetStorageDevice()
        {
            GuideHelper.Add(new ShowStorageDeviceSelector(FetchValue));
        }

        private void FetchValue(StorageDevice deviceArg)
        {
            if (device != null)
            {
                container.Dispose();
                device = deviceArg;
            }
            else
            {
                this.device = deviceArg;
            }

        }

        public void OpenContainer(string name)
        {
            if (name != null)
            {
                container = device.OpenContainer(name);
            }
        }

        public void OpenContainer()
        {
            container = device.OpenContainer(XnaHelpers.name);
        }

        public void CloseContainer()
        {
            container.Dispose();
        }

        public void DeleteContainer(string name)
        {
            device.DeleteContainer(name);
        }

        public void DeleteContainer()
        {
            device.DeleteContainer(XnaHelpers.name);
        }
    }
}
